package com.zmn.mcc.admin.controller.common;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.admin.controller.dept.DeptBaseController;
import com.zmn.mcc.model.dto.BodyDIO;
import com.zmn.mcc.model.entity.dept.McDept;
import com.zmn.mcc.model.entity.dept.McDeptQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author heguanghua
 * @since 2020/05/07 18:26
 */
@Controller
@RequestMapping("common/dept/")
public class CommonDeptListController extends DeptBaseController {

    /**
     * 部门联动
     *
     * @param request
     * @param response
     * @param parentId
     */
    @RequestMapping(value = "listChilds")
    @ResponseBody
    public ResponseDTO listChilds(HttpServletRequest request, HttpServletResponse response,
                                  @RequestBody BodyDIO<McDeptQuery> body) {

        if (body == null || body.getBody() == null) {
            return ResponseDTO.fail("空的部门列表");
        }

        McDeptQuery query = body.getBody();
        if (NumberUtil.isNullOrZero(query.getParentId())) {
            query.setParentId(GlobalConsts.TOP_ID);
        }

        List<McDept> depts = deptService.listByParentId(query.getOrgId(), query.getParentId(), GlobalDict.STATUS_ENABLE);

        List<VtDTO> items = new ArrayList<>();
        depts.forEach(item -> {
            items.add(new VtDTO(item.getDeptId(), item.getDeptName()));
        });

        return ResponseDTO.success(items, String.format("共%s个部门", items.size()));
    }

    /**
     * 描述: 加载部门树形结构
     *
     * @param orgId 机构id
     * @author guoqiao
     * @since 2021/3/3 10:28
     */
    @RequestMapping(value = "listDeptTree")
    @ResponseBody
    public ResponseDTO listDeptTree(@RequestParam(value = "orgId") Integer orgId) {

        try {
            List<McDept> deptList = deptService.listValidDept();
            List<McDept> mcDepts = buildTree(deptList, GlobalConsts.TOP_ID, orgId);
            return ResponseDTO.success(mcDepts, String.format("共%s个部门", mcDepts.size()));
        } catch (Exception e) {
            logger.error("查询失败,原因:{}", e.getMessage(), e);
            return ResponseDTO.fail("查询失败，程序异常");
        }
    }

    /**
     * 描述: 构建部门树形结构
     *
     * @param deptList 部门列表
     * @param deptId   需要递归查询部门id
     * @param orgId    机构id
     * @return List<com.zmn.mcc.model.entity.dept.McDept>
     * @author guoqiao
     * @since 2021/3/2 19:02
     */
    private List<McDept> buildTree(List<McDept> deptList, int deptId, int orgId) {
        List<McDept> treeList = new ArrayList<McDept>();
        for (McDept dept : deptList) {
            if (dept.getParentId() == deptId) {
                // 如果机构id不为空，则需要根据机构id加载部门
                if (NumberUtil.isNotNullOrZero(orgId) &&
                        (!Objects.equals(GlobalConsts.TOP_ID, dept.getParentId()) || !Objects.equals(orgId, dept.getOrgId()))) {
                    continue;
                }
                dept.setChildren(buildTree(deptList, dept.getDeptId(), 0));
                treeList.add(dept);
            }
        }
        return treeList;
    }

}
